//
//  YLHealthKitManager+Private.m
//  HealthKitManager
//
//  Created by JuneLee on 2018/11/13.
//

#import "YLHealthKitManager+Private.h"

#define HealthKitPlistKeyHeight        @"height"
#define HealthKitPlistKeyWeight        @"weight"
#define HealthKitPlistKeyBMI           @"bmi"
#define HealthKitPlistKeyBMR           @"bmr"
#define HealthKitPlistKeyBodyfat       @"bodyFat"
#define HealthKitPlistKeyFatWeight     @"fatWeight"
#define HealthKitPlistKeyStepCount     @"stepCount"
#define HealthKitPlistKeySleepAnalysis @"sleepAnalysis"
#define HealthKitPlistKeyHeartRate     @"heartRate"
#define HealthKitPlistKeyDistance      @"distance"

@implementation YLHealthKitManager (Private)

#pragma mark - 转换保存的数据模型
- (HKQuantitySample *)sampleObject:(YLHealthKitModel *)model healthKitType:(YLHealthKitType)healthKitType  API_AVAILABLE(ios(8.0)){
    
    HKQuantityTypeIdentifier identifier = [self getQuantityIdentifierWith:healthKitType];
    HKQuantityType *quantityType = [HKQuantityType quantityTypeForIdentifier:identifier];
    
    HKQuantity *quantity;
    switch (healthKitType) {
        case YLHealthKitTypeHeight:
            quantity = [HKQuantity quantityWithUnit:[HKUnit meterUnit] doubleValue:model.height];
            break;
        case YLHealthKitTypeWeight:
            quantity = [HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:model.weight];
            break;
        case YLHealthKitTypeFatWeight:
            quantity = [HKQuantity quantityWithUnit:[HKUnit gramUnit] doubleValue:model.fatWeight];
            break;
        case YLHealthKitTypeBodyFat:
            quantity = [HKQuantity quantityWithUnit:[HKUnit percentUnit] doubleValue:model.bodyFat];
            break;
        case YLHealthKitTypeBMI:
            quantity = [HKQuantity quantityWithUnit:[HKUnit percentUnit] doubleValue:model.bmi];
            break;
        case YLHealthKitTypeBMR:
            quantity = [HKQuantity quantityWithUnit:[HKUnit kilocalorieUnit] doubleValue:model.bmr];
            break;
        case YLHealthKitTypeStepCount:
            quantity = [HKQuantity quantityWithUnit:[HKUnit countUnit] doubleValue:model.step];
            break;
        case YLHealthKitTypeSleepAnalysis:
            break;
        case YLHealthKitTypeHeartRate:
            quantity = [HKQuantity quantityWithUnit:[[HKUnit countUnit] unitDividedByUnit:[HKUnit minuteUnit]] doubleValue:model.heartRate];
            break;
        default: break;
    }
    
    if (healthKitType == YLHealthKitTypeHeartRate || healthKitType == YLHealthKitTypeStepCount) {
        HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:quantityType quantity:quantity startDate:model.startDate endDate:model.endDate];
        return sample;
    }else {
        HKQuantitySample *sample = [HKQuantitySample quantitySampleWithType:quantityType quantity:quantity startDate:model.date endDate:model.date];
        return sample;
    }
}

#pragma mark - 处理睡眠数据
- (HKCategorySample *)sleepSample:(YLHealthKitModel *)model  API_AVAILABLE(ios(8.0)){
    
    HKCategoryType *sleepType = [HKCategoryType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
    HKCategoryValueSleepAnalysis sleepAnalysisType = [self getSleepValue:model.sleepType];
    HKCategorySample *sleepSample = [HKCategorySample categorySampleWithType:sleepType value:sleepAnalysisType startDate:model.startDate endDate:model.endDate metadata:nil];
    return sleepSample;
    
}

- (HKCategoryValueSleepAnalysis)getSleepValue:(SleepType)sleepType  API_AVAILABLE(ios(8.0)){
    
    HKCategoryValueSleepAnalysis sleepAnalysisType = HKCategoryValueSleepAnalysisInBed;
    if (@available(iOS 10.0, *)) {
        if (sleepType == SleepTypeInBed) {
            sleepAnalysisType = HKCategoryValueSleepAnalysisInBed;
        }else if (sleepType == SleepTypeAsleep){
            sleepAnalysisType = HKCategoryValueSleepAnalysisAsleep;
        }else{
            sleepAnalysisType = HKCategoryValueSleepAnalysisAwake;
        }
    }else{
        if (sleepType == SleepTypeAsleep){
            sleepAnalysisType = HKCategoryValueSleepAnalysisAsleep;
        }else{
            sleepAnalysisType = HKCategoryValueSleepAnalysisInBed;
        }
    }
    return sleepAnalysisType;
}

#pragma mark - 根据写入的数据，获取healthk的identifier
- (HKQuantityTypeIdentifier)getQuantityIdentifierWith:(YLHealthKitType)healthKitType {
    HKQuantityTypeIdentifier identifier;
    if (@available(iOS 8.0, *)) {
        switch (healthKitType) {
            case YLHealthKitTypeHeight: identifier = HKQuantityTypeIdentifierHeight; break;
            case YLHealthKitTypeWeight: identifier = HKQuantityTypeIdentifierBodyMass; break;
            case YLHealthKitTypeFatWeight: identifier = HKQuantityTypeIdentifierLeanBodyMass; break;
            case YLHealthKitTypeBodyFat: identifier = HKQuantityTypeIdentifierBodyFatPercentage; break;
            case YLHealthKitTypeBMI: identifier = HKQuantityTypeIdentifierBodyMassIndex; break;
            case YLHealthKitTypeBMR: identifier = HKQuantityTypeIdentifierBasalEnergyBurned; break;
            case YLHealthKitTypeStepCount: identifier = HKQuantityTypeIdentifierStepCount; break;
            case YLHealthKitTypeHeartRate: identifier = HKQuantityTypeIdentifierHeartRate; break;
            case YLHealthKitTypeSleepAnalysis: identifier = HKCategoryTypeIdentifierSleepAnalysis; break;
            case YLHealthKitTypeDistance: identifier = HKQuantityTypeIdentifierDistanceWalkingRunning; break;
            default: break;
        }
    }
    return identifier;
}

#pragma mark - 从plist中获取Read的类型
- (NSMutableSet *)getReadTypeFromPlist {
    NSDictionary *info = [NSBundle mainBundle].infoDictionary;
    NSDictionary *sharedReadTypeDic = info[@"YLHealthKitKey"];
    NSArray *shareAll = [sharedReadTypeDic valueForKey:@"share"];
    NSMutableSet *readSet = nil;
    int read = 0;
    if (sharedReadTypeDic) {
        if ([shareAll containsObject:HealthKitPlistKeyBMI]) {
            read = read | YLHealthKitTypeBMI;
        }
        if ([shareAll containsObject:HealthKitPlistKeyBMR]) {
            read = read | YLHealthKitTypeBMR;
        }
        if ([shareAll containsObject:HealthKitPlistKeyBodyfat]) {
            read = read | YLHealthKitTypeBodyFat;
        }
        if ([shareAll containsObject:HealthKitPlistKeyFatWeight]) {
            read = read | YLHealthKitTypeFatWeight;
        }
        if ([shareAll containsObject:HealthKitPlistKeyHeartRate]) {
            read = read | YLHealthKitTypeHeartRate;
        }
        if ([shareAll containsObject:HealthKitPlistKeyHeight]) {
            read = read | YLHealthKitTypeHeight;
        }
        if ([shareAll containsObject:HealthKitPlistKeyStepCount]) {
            read = read | YLHealthKitTypeStepCount;
        }
        if ([shareAll containsObject:HealthKitPlistKeyWeight]) {
            read = read | YLHealthKitTypeWeight;
        }
        if ([shareAll containsObject:HealthKitPlistKeySleepAnalysis]) {
            read = read | YLHealthKitTypeSleepAnalysis;
        }
        if ([shareAll containsObject:HealthKitPlistKeyDistance]) {
            read = read | YLHealthKitTypeDistance;
        }
    }
    if (read) {
        readSet = [self getAuthorizationType:read];
    }
    return readSet;
}

#pragma mark - 从plist中获取Shared的类型
- (NSMutableSet *)getSharedTypeFromPlist {
    NSDictionary *info = [NSBundle mainBundle].infoDictionary;
    NSDictionary *sharedReadTypeDic = info[@"YLHealthKitKey"];
    NSArray *readAll = [sharedReadTypeDic valueForKey:@"read"];
    NSMutableSet *sharedSet = nil;
    int shared = 0;
    if (sharedReadTypeDic) {
        if ([readAll containsObject:HealthKitPlistKeyBMI]) {
            shared = shared | YLHealthKitTypeBMI;
        }
        if ([readAll containsObject:HealthKitPlistKeyBMR]) {
            shared = shared | YLHealthKitTypeBMR;
        }
        if ([readAll containsObject:HealthKitPlistKeyBodyfat]) {
            shared = shared | YLHealthKitTypeBodyFat;
        }
        if ([readAll containsObject:HealthKitPlistKeyFatWeight]) {
            shared = shared | YLHealthKitTypeFatWeight;
        }
        if ([readAll containsObject:HealthKitPlistKeyHeartRate]) {
            shared = shared | YLHealthKitTypeHeartRate;
        }
        if ([readAll containsObject:HealthKitPlistKeyHeight]) {
            shared = shared | YLHealthKitTypeHeight;
        }
        if ([readAll containsObject:HealthKitPlistKeyStepCount]) {
            shared = shared | YLHealthKitTypeStepCount;
        }
        if ([readAll containsObject:HealthKitPlistKeyWeight]) {
            shared = shared | YLHealthKitTypeWeight;
        }
        if ([readAll containsObject:HealthKitPlistKeySleepAnalysis]) {
            shared = shared | YLHealthKitTypeSleepAnalysis;
        }
        if ([readAll containsObject:HealthKitPlistKeyDistance]) {
            shared = shared | YLHealthKitTypeDistance;
        }
    }
    if (shared) {
        sharedSet = [self getAuthorizationType:shared];
    }
    return sharedSet;
}

#pragma mark - 权限集合
- (NSMutableSet *)getAuthorizationType:(unsigned)type {
    NSMutableSet *typeSet = [NSMutableSet set];
    if (@available(iOS 8.0, *)) {
        if (type & YLHealthKitTypeHeight) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight]];
        }
        if (type & YLHealthKitTypeWeight) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass]];
        }
        if (type & YLHealthKitTypeFatWeight) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierLeanBodyMass]];
        }
        if (type & YLHealthKitTypeBodyFat) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyFatPercentage]];
        }
        if (type & YLHealthKitTypeBMI) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex]];
        }
        if (type & YLHealthKitTypeBMR) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBasalEnergyBurned]];
        }
        if (type & YLHealthKitTypeStepCount) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
        }
        if (type & YLHealthKitTypeHeartRate) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]];
        }
        if (type & YLHealthKitTypeSleepAnalysis) {
            [typeSet addObject:[HKCategoryType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis]];
        }
        if (type & YLHealthKitTypeDistance) {
            [typeSet addObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierDistanceWalkingRunning]];
        }
        return typeSet;
    }
    return typeSet;
}
@end
